package com.xyebank.ledger.service.common;

import com.xyebank.ledger.db.entitys.LApplicationInf;
import com.xyebank.ledger.db.entitys.LPaymentInf;
import com.xyebank.ledger.db.entitys.LRepaymentInf;
import com.xyebank.ledger.enums.LedgerJobType;
import com.xyebank.ledger.service.common.conveter.ApplyBeanConverter;
import com.xyebank.ledger.service.common.conveter.PayBeanConverter;
import com.xyebank.ledger.service.common.conveter.RePayBeanConverter;
import com.xyebank.ledger.util.SpecialArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 原始台帐转换成标准台帐处理业务--标准流程
 * @param <A>　application　申请
 * @param <P>  payment　放款
 * @param <R> repayment 还款
 */
@Service
public abstract class UnionServiceAdapter<A, P, R> implements UnionService<A, P, R>{
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    UnionLedgerDAO unionLedgerDAO;

    @Autowired
    ApplyBeanConverter<A> applyBeanConver;

    @Autowired
    PayBeanConverter<P> payBeanConver;

    @Autowired
    RePayBeanConverter<R> rePayBeanConver;

    public Integer dealApplication(Integer fromIdx, List<A> list, LedgerJobType jobType) {
        if (list == null || list.size() == 0) {
            logger.info("没有更多数据了");
            return 0;
        }
        long beginTime = System.currentTimeMillis();
        boolean isMig = false; //区分是数据库迁移还是新增任务 Vo有id表示数据库迁移
        String tempStr = "实时" + getTitle();
        if (jobType == LedgerJobType.MIGRATE) {
            isMig = true;
            tempStr = "迁移" + getTitle();
        }
        logger.info("{}开始", tempStr);
        List<LApplicationInf> resultList = new SpecialArrayList<>();
        for (A e : list) {
//            LApplicationInf targetEntity = getApplyBC().covert(e);
            LApplicationInf targetEntity = applyBeanConver.covert(e);
            //TODO 其他无法映射字段处理
            resultList.add(targetEntity);
        }

        int sucNum = unionLedgerDAO.insertLApplicationInf(tempStr, isMig, resultList, list);
        long endTime = System.currentTimeMillis();
        long excutedTime = endTime - beginTime;
        logger.info("{}分批处理结束,总记录:{},成功:{},失败:{},耗时:{}ms", tempStr, list.size(), sucNum, list.size() - sucNum, excutedTime);
        return sucNum;
    }

    public Integer dealPayment(Integer fromIdx, List<P> list, LedgerJobType jobType) {
        if (list == null || list.size() == 0) {
            logger.info("没有更多数据了");
            return 0;
        }
        long beginTime = System.currentTimeMillis();
        boolean isMig = false; //区分是数据库迁移还是新增任务 Vo有id表示数据库迁移
        String tempStr = "实时" + getTitle();
        if (jobType == LedgerJobType.MIGRATE) {
            isMig = true;
            tempStr = "迁移" + getTitle();
        }
        logger.info("{}开始", tempStr);
        List<LPaymentInf> resultList = new SpecialArrayList<>();
        for (P e : list) {
//            LPaymentInf targetEntity = getPayBC().covert(e);
            LPaymentInf targetEntity = payBeanConver.covert(e);
            //TODO 其他无法映射字段处理
            resultList.add(targetEntity);
        }

        int sucNum = unionLedgerDAO.insertLPaymentInf(tempStr, isMig, resultList, list);
        long endTime = System.currentTimeMillis();
        long excutedTime = endTime - beginTime;
        logger.info("{}分批处理结束,总记录:{},成功:{},失败:{},耗时:{}ms", tempStr, list.size(), sucNum, list.size() - sucNum, excutedTime);
        return sucNum;
    }

    public Integer dealRePayment(Integer fromIdx, List<R> list, LedgerJobType jobType) {
        if (list == null || list.size() == 0) {
            logger.info("没有更多数据了");
            return 0;
        }
        long beginTime = System.currentTimeMillis();
        boolean isMig = false; //区分是数据库迁移还是新增任务 Vo有id表示数据库迁移
        String tempStr = "实时" + getTitle();
        if (jobType == LedgerJobType.MIGRATE) {
            isMig = true;
            tempStr = "迁移" + getTitle();
        }
        logger.info("{}开始", tempStr);
        List<LRepaymentInf> resultList = new SpecialArrayList<>();
        for (R e : list) {
            LRepaymentInf targetEntity = rePayBeanConver.covert(e);
            //TODO 其他无法映射字段处理
            resultList.add(targetEntity);
        }

        int sucNum = unionLedgerDAO.insertLRePaymentInf(tempStr, isMig, resultList, list);
        long endTime = System.currentTimeMillis();
        long excutedTime = endTime - beginTime;
        logger.info("{}分批处理结束,总记录:{},成功:{},失败:{},耗时:{}ms", tempStr, list.size(), sucNum, list.size() - sucNum, excutedTime);
        return sucNum;
    }


    public abstract String getTitle();

//    public abstract ApplyBeanConverter<A> getApplyBC();
//    public abstract PayBeanConverter<P> getPayBC();
//    public abstract RePayBeanConverter<R> getRePayBC();

//    public abstract TransactionTemplate getApplyTT();
//    public abstract TransactionTemplate getPayTT();
//    public abstract TransactionTemplate getRePayTT();


//
//    public static class None {
//    }
}
